## The type of library we want to build. Possible values:
##   relocatable
##   static
LIBRARY_TYPE=static

# You may edit this makefile as long as you keep these original 
# target names defined.
MODE=distrib
GNATMAKE=@GNATMAKE@
GNATCLEAN=gnatclean
SVN=svn
INSTALL=@INSTALL@

GPRPATH=util.gpr
UTIL_HTTP_GPRPATH=util_http.gpr
UTIL_HTTP_CURL_GPRPATH=util_http_curl.gpr
UTIL_HTTP_AWS_GPRPATH=util_http_aws.gpr
TESTUTIL_GPRPATH=util_unit.gpr

BUILDS_SHARED=@BUILDS_SHARED@

version=@UTIL_VERSION@

distdir=ada-util-@UTIL_VERSION@

DIST_FILE=ada-util-@UTIL_VERSION@.tar.gz

XML_SRC_DIR=@UTIL_XML_DIR@
ASM_SRC_DIR=@UTIL_ASM_DIR@
OS_SRC_DIR=@UTIL_OS_DIR@
OS_VERSION=@UTIL_OS_VERSION@
AUNIT_SRC_DIR=@UTIL_AUNIT_DIR@
AHVEN_SRC_DIR=@UTIL_AHVEN_DIR@
AHVEN_SRC_OS_DIR=@UTIL_AHVEN_OS_DIR@

HAVE_XML=@UTIL_HAVE_XML@
HAVE_CURL=@UTIL_HAVE_CURL@
HAVE_AWS=@UTIL_HAVE_AWS@
HAVE_VECTOR_MAPPERS=@UTIL_HAVE_VECTOR_MAPPERS@

LN_S=@LN_S@
MKDIR=mkdir
CP=cp
LN=ln -s
CC=@CC@
CFLAGS=@CFLAGS@ @DEFS@

ifeq (${OS},Windows_NT)
LIBEXT=dll
LIBVER=dll
else
LIBEXT=so
LIBVER=so.$(version)
endif

srcdir = .
top_srcdir = @top_srcdir@
VPATH = @srcdir@
prefix = @prefix@
exec_prefix = @exec_prefix@
top_builddir = .

includedir=${prefix}/share/ada/adainclude
projectdir=${prefix}/share/ada/adainclude
bindir=${prefix}/bin
libdir=${prefix}/lib
alidir=${libdir}/ada/adalib/

libname=libada_util
libprefix=$(libname)_

CPUS := @NR_CPUS@
MAKE_ARGS=-XMODE=${MODE} -XLIBRARY_TYPE=${LIBRARY_TYPE} -XPROCESSORS=$(CPUS)

# The timeout execution time in second for a test case.
# The concurrent fifo test takes arround 120 seconds on some ARM but only 4 seconds on an i7.
# Make this a make configuration variable so that it can be given when launching make.
TEST_TIMEOUT=30

ifeq (${BUILDS_SHARED},yes)
all:     static shared
install: install_static install_shared
else
all:     static
install: install_static
endif

UTIL_GEN_FILES=src/util-systems-constants.ads
ifeq (${HAVE_CURL},yes)
UTIL_GEN_FILES+=src/http/curl/util-http-clients-curl-constants.ads
endif

# Build executables for all mains defined by the project.
build:	$(UTIL_GEN_FILES)
	$(GNATMAKE) -m -p -P"$(GPRPATH)" $(MAKE_ARGS)
	test T${HAVE_CURL} = Tno || $(GNATMAKE) -m -p -P"$(UTIL_HTTP_CURL_GPRPATH)" $(MAKE_ARGS)
	test T${HAVE_AWS} = Tno || $(GNATMAKE) -m -p -P"$(UTIL_HTTP_AWS_GPRPATH)" $(MAKE_ARGS)
	$(GNATMAKE) -m -p -P"$(TESTUTIL_GPRPATH)" $(MAKE_ARGS)
	$(GNATMAKE) -m -p -P"$(UTIL_HTTP_GPRPATH)" $(MAKE_ARGS)

static:
	$(MAKE) LIBRARY_TYPE=static build

shared:
	$(MAKE) LIBRARY_TYPE=relocatable build

# Build and run the unit tests
test:	build regtests/util-testsuite.adb
	$(GNATMAKE) -p -Ptests_proc $(MAKE_ARGS)
	$(GNATMAKE) -p -Ptests $(MAKE_ARGS)
	-bin/util_harness -xml util-aunit.xml -timeout ${TEST_TIMEOUT}

regtests/util-testsuite.adb: regtests/util-testsuite.gpb Makefile
	gnatprep -DHAVE_XML=$(HAVE_XML) -DHAVE_CURL=$(HAVE_CURL) \
                 -DHAVE_AWS=$(HAVE_AWS) -DHAVE_VECTOR_MAPPERS=$(HAVE_VECTOR_MAPPERS) \
		 -DOS_VERSION='"$(OS_VERSION)"' \
		 regtests/util-testsuite.gpb $@

# Not intended for manual invocation.
# Invoked if automatic builds are enabled.
# Analyzes only on those sources that have changed.
# Does not build executables.
autobuild:
	$(GNATMAKE) -gnatc -c -k  -P "$(GPRPATH)"

# Clean the root project of all build products.
clean:	clean_test
	-rm -rf obj/* $(UTIL_GEN_FILES) bin/utilgen
	-rm -rf lib/util
	-rm -rf lib/util-curl
	-rm -rf lib/util-aws
	-rm -rf lib/testutil
	-rm -f bin/util_harness bin/util_test_process

# Clean the files produced by the unit tests
clean_test:
	rm -f test?.log test.log test-stream.txt test-write.txt util-tests.xml
	rm -rf regtests/result

# Clean root project and all imported projects too.
clean_tree:
	$(GNATCLEAN) -P "$(GPRPATH)" -r

# Check *all* sources for errors, even those not changed.
# Does not build executables.
analyze:
	$(GNATMAKE) -f  -gnatc -c -k  -P "$(GPRPATH)"

# Clean, then build executables for all mains defined by the project.
rebuild: clean build

# Update src/util.ads with the latest SVN revision number.
version:
	gnatprep `svn info | grep '^[UR][eR][Lv]' | sed -e 's,URL: \(.*\),-DURL="\1",' -e 's,Revision: ,-DREVISION=,'` \
		  src/util.gpb src/util.ads

# Files to package in source distribution
DISTFILES=README LICENSE.txt LICENSE.Ahven NOTICE.txt NEWS CHANGES ChangeLog \
config.guess configure config.sub \
configure.in Makefile.in install-sh \
util.gpr.in util_http.gpr.in config.gpr util_unit.gpr.in tests.gpr tests_proc.gpr samples.gpr \
util_http_aws.gpr.in util_http_curl.gpr.in \
src samples regtests testutil \
distrib support bundles debian

dist:
	-rm -rf $(distdir)
	mkdir $(distdir)
	-chmod 777 $(distdir)
	here=`cd $(top_builddir) && pwd`; \
	top_distdir=`cd $(distdir) && pwd`; \
	distdir=`cd $(distdir) && pwd`; \
	for file in $(DISTFILES); do \
	  d=$(srcdir); \
	  if test -d $$d/$$file; then \
	    cp -pr $$d/$$file $(distdir)/$$file; \
	  else \
	    test -f $(distdir)/$$file \
	    || ln $$d/$$file $(distdir)/$$file 2> /dev/null \
	    || cp -p $$d/$$file $(distdir)/$$file || :; \
	  fi; \
	done
	rm -f $(distdir)/distrib/*.gpr
	rm -rf $(distdir)/regtests/result
	mkdir $(distdir)/bin
	mkdir $(distdir)/obj
	mkdir $(distdir)/lib
	tar --exclude=.svn --exclude="*~" --exclude="*.o" --exclude="*.ali" \
	    --exclude="*.log" --exclude="*.a" \
	    -cvzf $(DIST_FILE) $(distdir)

install_static:
	$(MAKE) LIBRARY_TYPE=static install_lib

install_shared:
	$(MAKE) LIBRARY_TYPE=relocatable install_lib

install_lib:
	rm -rf ${alidir}/ada-util-curl \
	       ${alidir}/ada-util-aws \
	       ${alidir}/ada-util-unit \
	       ${alidir}/ada-util
	$(MKDIR) -p ${includedir}/ada-util
	$(MKDIR) -p ${includedir}/ada-util-unit
	$(MKDIR) -p ${projectdir}/
	${MKDIR} -p ${alidir}/ada-util-unit
	${MKDIR} -p ${alidir}/ada-util
	${MKDIR} -p ${libdir} ${bindir}
	${CP} -p testutil/*.ad[bs] ${includedir}/ada-util-unit
	${CP} -p src/*.ad[bs] ${includedir}/ada-util
	${CP} -p src/http/*.ad[bs] ${includedir}/ada-util
	if test T${XML_SRC_DIR} != T ; then \
	   ${CP} -p ${XML_SRC_DIR}/*.ad[bs] ${includedir}/ada-util ; \
	fi
	if test T${ASM_SRC_DIR} != T ; then \
	   ${CP} -p ${ASM_SRC_DIR}/*.ad[bs] ${includedir}/ada-util ; \
	fi
	if test T${OS_SRC_DIR} != T ; then \
	   ${CP} -p ${OS_SRC_DIR}/*.ad[bs] ${includedir}/ada-util ; \
	fi
	if test T${AUNIT_SRC_DIR} != T ; then \
	   ${CP} -p ${AUNIT_SRC_DIR}/*.ad[bs] ${includedir}/ada-util-unit ; \
	fi
	if test T${AHVEN_SRC_DIR} != T ; then \
	   ${CP} -p ${AHVEN_SRC_DIR}/*.ad[bs] ${includedir}/ada-util-unit ; \
	   ${CP} -p ${AHVEN_SRC_OS_DIR}/*.ad[bs] ${includedir}/ada-util-unit ; \
	fi
	sed -e "s,INSTALL,${prefix},g" -e "s,LIBRARY_TYPE,$(LIBRARY_TYPE),g" < distrib/util.gpr > ${projectdir}/util.gpr
	sed -e "s,INSTALL,${prefix},g" -e "s,LIBRARY_TYPE,$(LIBRARY_TYPE),g" < distrib/util_unit.gpr > ${projectdir}/util_unit.gpr
	sed -e "s,INSTALL,${prefix},g" -e "s,LIBRARY_TYPE,$(LIBRARY_TYPE),g" < distrib/util_http.gpr > ${projectdir}/util_http.gpr
	test T${HAVE_CURL} = Tno || \
	     sed -e "s,INSTALL,${prefix},g" -e "s,LIBRARY_TYPE,$(LIBRARY_TYPE),g" \
		 < distrib/util_http_curl.gpr > ${projectdir}/util_http_curl.gpr
	test T${HAVE_AWS} = Tno || \
	     sed -e "s,INSTALL,${prefix},g" -e "s,LIBRARY_TYPE,$(LIBRARY_TYPE),g" \
		 < distrib/util_http_aws.gpr > ${projectdir}/util_http_aws.gpr
	test T${HAVE_CURL} = Tno || \
	  (${MKDIR} -p ${includedir}/ada-util-curl && \
           ${MKDIR} -p ${alidir}/ada-util-curl && \
	   ${CP} -p src/http/curl/*.ad[bs] ${includedir}/ada-util-curl && \
	   ${CP} -p lib/util-curl/$(LIBRARY_TYPE)/*.ali ${alidir}/ada-util-curl)
	test T${HAVE_AWS} = Tno || \
	  (${MKDIR} -p ${includedir}/ada-util-aws && \
           ${MKDIR} -p ${alidir}/ada-util-aws && \
	   ${CP} -p src/http/aws/*.ad[bs] ${includedir}/ada-util-aws && \
	   ${CP} -p lib/util-aws/$(LIBRARY_TYPE)/*.ali ${alidir}/ada-util-aws)
	${CP} -p lib/testutil/$(LIBRARY_TYPE)/*.ali ${alidir}/ada-util-unit
	${CP} -p lib/util/$(LIBRARY_TYPE)/*.ali ${alidir}/ada-util
ifeq ($(LIBRARY_TYPE),static)
	test T${HAVE_AWS} = Tno || \
	   ${CP} -p lib/util-aws/$(LIBRARY_TYPE)/$(libprefix)aws.a ${libdir}/
	test T${HAVE_CURL} = Tno || \
	   ${CP} -p lib/util-curl/$(LIBRARY_TYPE)/$(libprefix)curl.a ${libdir}/
	${CP} -p lib/util/$(LIBRARY_TYPE)/$(libname).a ${libdir}/
	${CP} -p lib/testutil/$(LIBRARY_TYPE)/$(libprefix)unit.a ${libdir}/
else
	test T${HAVE_AWS} = Tno || \
	   ${CP} -p lib/util-aws/$(LIBRARY_TYPE)/$(libprefix)aws.$(LIBVER) ${libdir}/
	test T${HAVE_CURL} = Tno || \
	   ${CP} -p lib/util-curl/$(LIBRARY_TYPE)/$(libprefix)curl.$(LIBVER) ${libdir}/
	${CP} -p lib/util/$(LIBRARY_TYPE)/$(libname).$(LIBVER) ${libdir}/
	${CP} -p lib/testutil/$(LIBRARY_TYPE)/$(libprefix)unit.$(LIBVER) ${libdir}/
ifneq (${OS},Windows_NT)
	cd ${libdir} && \
	rm -f $(libname).$(LIBEXT) $(libprefix)unit.$(LIBEXT) && \
	rm -f $(libprefix)aws.$(LIBEXT) $(libprefix)curl.$(LIBEXT) && \
	${LN} $(libname).$(LIBVER) $(libname).$(LIBEXT) && \
	${LN} $(libprefix)unit.$(LIBVER) $(libprefix)unit.$(LIBEXT) && \
	(test T${HAVE_AWS} = Tno || \
	   ${LN} $(libprefix)aws.$(LIBVER) $(libprefix)aws.$(LIBEXT)) && \
	(test T${HAVE_CURL} = Tno || \
	   ${LN} $(libprefix)curl.$(LIBVER) $(libprefix)curl.$(LIBEXT))
else
	${CP} -p lib/util/$(LIBRARY_TYPE)/$(libname).$(LIBVER) ${bindir}/
	${CP} -p lib/testutil/$(LIBRARY_TYPE)/$(libprefix)unit.$(LIBVER) ${bindir}/
endif
endif


install-support:
	$(MKDIR) -p ${bindir}
	${CP} support/*.sh ${bindir}
	${CP} support/*.xsl ${bindir}

uninstall:
	rm -rf ${includedir}/ada-util ${includedir}/ada-util-unit ${includedir}/ada-util-curl ${includedir}/ada-util-aws
	rm -rf ${alidir}/ada-util ${alidir}/ada-util-unit ${alidir}/ada-util-curl ${alidir}/ada-util-aws  ${alidir}/ada-util-unit
	rm -f ${libdir}/$(libname).a
	rm -f ${libdir}/$(libprefix)curl.a ${libdir}/$(libprefix)curl.$(LIBEXT)  ${libdir}/$(libprefix)curl.$(LIBEXT).${version}
	rm -f ${libdir}/$(libprefix)aws.a ${libdir}/$(libprefix)aws.$(LIBEXT) ${libdir}/$(libprefix)aws.$(LIBEXT).${version}
	rm -f ${libdir}/$(libprefix)unit.a ${libdir}/$(libprefix)unit.$(LIBEXT) ${libdir}/$(libprefix)unit.$(LIBEXT).${version}
	rm -f ${libdir}/$(libname).so.${version}
	rm -f ${libdir}/$(libprefix)unit.so.${version}
	rm -f ${projectdir}/util_http_aws.gpr
	rm -f ${projectdir}/util_http_curl.gpr
	rm -f ${projectdir}/util_http.gpr
	rm -f ${projectdir}/util_unit.gpr
	rm -f ${projectdir}/util.gpr

# prepare before releasing a new version of the package
# (collect information about changes and SVN repository)
prepare-release:
	${SVN} update
	${SVN} info > CHANGES
	${SVN} log -v > ChangeLog

src/util-systems-constants.ads:	bin/utilgen
	bin/utilgen > $@

src/http/curl/util-http-clients-curl-constants.ads:	bin/utilgen
	bin/utilgen curl > $@

# Utility for the generation of util-systems-constants.ads
bin/utilgen:    support/utilgen.c Makefile
	mkdir -p bin
	$(CC) -o $@ $(CFLAGS) -g support/utilgen.c
